# coding=utf-8
#


from django.db import models

from datetime import datetime


class IDC(models.Model):
    """ 运营商 """
    name = models.CharField(max_length=32, verbose_name="运营商名称")
    cn_name = models.CharField(max_length=32, verbose_name="运营商中文名", default="")
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "云厂商"
        verbose_name_plural = "云厂商管理"

    def __str__(self):
        return self.name



class Region(models.Model):
    """ Region """

    REGION_STATUS_CHOICES = ((1, "启用中"),
                             (0, "已停用"))

    idc = models.ForeignKey(IDC, verbose_name="运营商", on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=64, verbose_name="Region名")
    rid = models.CharField(max_length=128, verbose_name="RegionID")
    status = models.IntegerField(default=0, verbose_name="AZone状态", choices=REGION_STATUS_CHOICES)
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "云机房Region"
        verbose_name_plural = "云机房Region管理"

    def __str__(self):
        return self.name


class AZone(models.Model):
    """ AZone """

    AZONE_STATUS_CHOICES = ((1, "启用中"),
                            (0, "已停用"))

    name = models.CharField(max_length=64, verbose_name="AZone名")
    region = models.ForeignKey(Region, verbose_name="Region", on_delete=models.SET_NULL, null=True, blank=True)
    status = models.IntegerField(default=0, verbose_name="AZone状态", choices=AZONE_STATUS_CHOICES)
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "可用区AZone"
        verbose_name_plural = "可用区AZone管理"

    def __str__(self):
        return self.name


class ECS(models.Model):
    """ 机器实例 """

    BTYPE_CHOICES = ((1, "独享"),
                     (2, "共享"))

    idc = models.ForeignKey(IDC, verbose_name="云厂商", on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=128, verbose_name="实例名称")
    instance_id = models.CharField(max_length=128, verbose_name="实例ID")
    azone = models.ForeignKey(AZone, verbose_name="AZone", on_delete=models.SET_NULL, null=True, blank=True)
    region = models.ForeignKey(Region, verbose_name="Region", on_delete=models.SET_NULL, null=True, blank=True)
    instance_type = models.CharField(max_length=128, verbose_name="机器规格")
    cpu = models.IntegerField(default=0, verbose_name="CPU核心")
    mem = models.IntegerField(default=0, verbose_name="内存")
    image = models.CharField(max_length=64, verbose_name="镜像")
    system = models.CharField(max_length=128, verbose_name="操作系统")
    inner_ip = models.CharField(max_length=64, verbose_name="私有IP")
    pub_ip = models.CharField(max_length=64, verbose_name="公网IP")
    bandwidth = models.CharField(max_length=64, verbose_name="带宽")
    btype = models.IntegerField(default=1, verbose_name="带宽类型", choices=BTYPE_CHOICES)
    vpc = models.CharField(max_length=64, verbose_name="虚拟私有云")
    ctime = models.DateTimeField(verbose_name="创建时间")
    fee_type = models.CharField(max_length=32, verbose_name="计费方式", default="")
    expire_time = models.DateTimeField(default=datetime(2000, 1, 1), verbose_name="到期时间")
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "ECS实例"
        verbose_name_plural = "ECS实例管理"

    def __str__(self):
        return self.name

